<!--
 * @Description: 
 * @Author: wgy
 * @CreateTime: 2022-12-18 16:35:33
 * @UpdateTime: 2022-12-18 22:29:36
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 定义一个容器 -->
    <div id="box"></div>
    <div id="box1"></div>
    <div id="box2"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-domm库 用于操作dom -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入props库 用于操作props -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">
        // 创建一个类组件
        class MyComponent extends React.Component {
            render() {
                // 读取状态
                const { name, sex, age, speak } = this.props
                return <h1 onClick={speak}>这是我的第一个props使用,我的名字是{name},我的性别是{sex},今年{age}</h1>
            }
        }

        // 对属性进行分类.必要限制
        MyComponent.propTypes = {
            name: PropTypes.string.isRequired, // 名字为字符串类型且必填
            age: PropTypes.number,  // 年龄为数字类型
            sex: PropTypes.string, // 性别为字符串类型
            speak: PropTypes.func  // 方法为函数类型
        }
        // 设置属性默认值
        MyComponent.defaultProps = {
            sex: '不男不女',
            age: 18
        }

        // 组件传入对象
        class MyObjComponent extends React.Component {
            render() {
                // 读取状态
                const { name, sex, age } = this.props
                return (
                    <div>
                        <ul>
                            <li>{name}</li>
                            <li>{age + 1}</li>
                            <li>{sex}</li>
                        </ul>
                    </div>
                )
            }
        }

        function speak() {
            console.log('我很厉害吧');
            
        }

        // 渲染元素
        ReactDOM.render(<MyComponent name="wgy" sex="男" speak={speak} />, document.getElementById("box"))
        ReactDOM.render(<MyComponent name="why" />, document.getElementById("box1"))


        // 批量添加
        let obj = { name: "小吴", sex: "男", age: 18 }
        ReactDOM.render(<MyObjComponent {...obj} />, document.getElementById("box2"))
    </script>
</body>

</html>